
package pl.com.qapps.datastore.berkeley;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.google.apphosting.api.DatastorePb.GetRequest;
import com.google.apphosting.api.DatastorePb.GetResponse;
import com.google.apphosting.api.DatastorePb.GetResponse.Entity;
import com.google.apphosting.api.DatastorePb.PutRequest;
import com.google.apphosting.api.DatastorePb.PutResponse;
import com.google.apphosting.api.DatastorePb.Transaction;
import com.google.storage.onestore.v3.OnestoreEntity.EntityProto;
import com.google.storage.onestore.v3.OnestoreEntity.Reference;

/**
 * @author Jakub Dykowski
 *
 */
public class MemoryFileStore implements FileStore {

	private final Map<Reference, EntityProto> store = new HashMap<>();
	
	@Override
	public void close() throws IOException {
		store.clear();
	}

	@Override
	public GetResponse make(GetRequest req) {
		GetResponse res = new GetResponse();
		Transaction txn = req.getTransaction();
		for (Reference key : req.keys()) {
			EntityProto proto = store.get(key);
			Entity entity = new Entity();
			entity.setEntity(proto);
			res.addEntity(entity);
		}
		return res;
	}

	@Override
	public PutResponse make(PutRequest req) {
		PutResponse res = new PutResponse();
		for (EntityProto entity : req.entitys()) {
			Reference key = entity.getKey();
			store.put(key, entity);
			res.addKey(key);
		}
		return res;
	}

}
